CF:219A. k-String

本文介绍了一种基于字符频率的算法,该算法检查输入字符串中每个不同字符的数量是否可以被给定的整数K整除。如果可以,则输出由这些字符组成的K阶重复字符串;否则输出“-1”。文章通过C++实现这一逻辑,并展示了完整的代码。

每个不同字符的个数若能被阶数K整除,则能输出K阶相同字符串相连,否则不能,则输出“-1”。

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
char s[1010],b[1010];
int a[1010];
int main(){
    int k;
    while(cin>>k){
        cin>>s;
        memset(a,0,sizeof(a));
        int len=strlen(s);
        sort(s,s+len);
        int count=1;
        int i,j,c,t=1;
        for(i=0;i<len;i++){
            if(s[i]==s[i+1])
                count++;
            else{
                if(count%k==0) {a[i]=count;count = 1;}
                else {cout<<"-1"<<'\n';t=0;break;}
            }
        }
        int x=0;
        if(t){
            for(j=0;j<len;j++){
                if(a[j]!=0){
                        for(c=1;c<=a[j]/k;++c)
                            b[x++]=s[j];
                }
            }
            for(i=0;i<k;++i){
                cout<<b;
            }
            cout<<'\n';
        }
    }
    return 0;
}


 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>图书管理系统</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Arial', sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; padding: 20px; } .container { max-width: 1200px; margin: 0 auto; background: white; border-radius: 15px; box-shadow: 0 20px 40px rgba(0,0,0,0.1); overflow: hidden; } .header { background: linear-gradient(135deg, #2c3e50, #34495e); color: white; padding: 30px; text-align: center; } .header h1 { font-size: 2.5em; margin-bottom: 10px; } .main-content { padding: 30px; } .form-section { background: #f8f9fa; padding: 25px; border-radius: 10px; margin-bottom: 30px; border-left: 5px solid #3498db; } .form-group { margin-bottom: 20px; } .form-group label { display: block; margin-bottom: 8px; font-weight: bold; color: #2c3e50; } .form-control { width: 100%; padding: 12px; border: 2px solid #e9ecef; border-radius: 8px; font-size: 16px; transition: all 0.3s ease; } .form-control:focus { border-color: #3498db; box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1); outline: none; } .btn { padding: 12px 25px; border: none; border-radius: 8px; cursor: pointer; font-size: 16px; font-weight: bold; transition: all 0.3s ease; } .btn-primary { background: linear-gradient(135deg, #3498db, #2980b9); color: white; } .btn-primary:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(52, 152, 219, 0.4); } .btn-danger { background: linear-gradient(135deg, #e74c3c, #c0392b); color: white; } .btn-success { background: linear-gradient(135deg, #27ae60, #219a52); color: white; } .books-section { margin-top: 30px; } .books-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; margin-top: 20px; } .book-card { background: white; border-radius: 12px; padding: 20px; box-shadow: 0 5px 15px rgba(0,0,0,0.08); border: 1px solid #e9ecef; transition: all 0.3s ease; } .book-card:hover { transform: translateY(-5px); box-shadow: 0 10px 25px rgba(0,0,0,0.15); } .book-title { font-size: 1.3em; font-weight: bold; color: #2c3e50; margin-bottom: 10px; } .book-info { color: #7f8c8d; margin-bottom: 8px; } .book-actions { margin-top: 15px; display: flex; gap: 10px; } .search-section { margin-bottom: 25px; } .search-box { display: flex; gap: 10px; } .alert { padding: 15px; border-radius: 8px; margin-bottom: 20px; } .alert-success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; } .alert-error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; } </style> </head> <body> <div class="container"> <div class="header"> <h1>📚 图书管理系统</h1> <p>轻松管理您的图书收藏</p> </div> <div class="main-content"> <!-- 添加/编辑图书表单 --> <div class="form-section"> <h2><%= request.getParameter("editId") != null ? "编辑图书" : "添加新图书" %></h2> <form action="bookAction.jsp" method="post"> <input type="hidden" name="action" value="<%= request.getParameter("editId") != null ? "update" : "add" %>"> <input type="hidden" name="id" value="<%= request.getParameter("editId") != null ? request.getParameter("editId") : "" %>"> <div class="form-group"> <label for="title">图书名称</label> <input type="text" id="title" name="title" class="form-control" value="<%= request.getParameter("editTitle") != null ? request.getParameter("editTitle") : "" %>" required> </div> <div class="form-group"> <label for="author">作者</label> <input type="text" id="author" name="author" class="form-control" value="<%= request.getParameter("editAuthor") != null ? request.getParameter("editAuthor") : "" %>" required> </div> <div class="form-group"> <label for="isbn">ISBN</label> <input type="text" id="isbn" name="isbn" class="form-control" value="<%= request.getParameter("editIsbn") != null ? request.getParameter("editIsbn") : "" %>" required> </div> <div class="form-group"> <label for="price">价格</label> <input type="number" id="price" name="price" step="0.01" class="form-control" value="<%= request.getParameter("editPrice") != null ? request.getParameter("editPrice") : "" %>" required> </div> <button type="submit" class="btn btn-primary"> <%= request.getParameter("editId") != null ? "更新图书" : "添加图书" %> </button> <% if (request.getParameter("editId") != null) { %> <a href="index.jsp" class="btn" style="background: #95a5a6; color: white;">取消</a> <% } %> </form> </div> <!-- 搜索区域 --> <div class="search-section"> <form action="index.jsp" method="get"> <div class="search-box"> <input type="text" name="search" class="form-control" placeholder="搜索图书名称或作者..." value="<%= request.getParameter("search") != null ? request.getParameter("search") : "" %>"> <button type="submit" class="btn btn-primary">搜索</button> <a href="index.jsp" class="btn" style="background: #95a5a6; color: white;">重置</a> </div> </form> </div> <!-- 图书列表 --> <div class="books-section"> <h2>图书列表</h2> <div class="books-grid"> <% // 模拟数据 - 在实际应用中应该从数据库获取 List<Map<String, String>> books = new ArrayList<>(); Map<String, String> book1 = new HashMap<>(); book1.put("id", "1"); book1.put("title", "Java编程思想"); book1.put("author", "Bruce Eckel"); book1.put("isbn", "9787111213826"); book1.put("price", "108.00"); books.add(book1); Map<String, String> book2 = new HashMap<>(); book2.put("id", "2"); book2.put("title", "深入理解计算机系统"); book2.put("author", "Randal E. Bryant"); book2.put("isbn", "9787111544937"); book2.put("price", "139.00"); books.add(book2); %> <% for (Map<String, String> book : books) { %> <div class="book-card"> <div class="book-title"><%= book.get("title") %></div> <div class="book-info"><strong>作者:</strong> <%= book.get("author") %></div> <div class="book-info"><strong>ISBN:</strong> <%= book.get("isbn") %></div> <div class="book-info"><strong>价格:</strong> ¥<%= book.get("price") %></div> <div class="book-actions"> <a href="index.jsp?editId=<%= book.get("id") %>&editTitle=<%= book.get("title") %>&editAuthor=<%= book.get("author") %>&editIsbn=<%= book.get("isbn") %>&editPrice=<%= book.get("price") %>" class="btn btn-success">编辑</a> <a href="bookAction.jsp?action=delete&id=<%= book.get("id") %>" class="btn btn-danger" onclick="return confirm('确定要删除这本书吗?')">删除</a> </div> </div> <% } %> </div> </div> </div> </div> <script> // 简单的表单验证 document.querySelector('form').addEventListener('submit', function(e) { const title = document.getElementById('title').value.trim(); const author = document.getElementById('author').value.trim(); const isbn = document.getElementById('isbn').value.trim(); const price = document.getElementById('price').value.trim(); if (!title || !author || !isbn || !price) { e.preventDefault(); alert('请填写所有必填字段!'); return false; } if (isNaN(price) || parseFloat(price) <= 0) { e.preventDefault(); alert('请输入有效的价格!'); return false; } }); // 显示操作结果消息 <% String message = request.getParameter("message"); String messageType = request.getParameter("messageType"); if (message != null) { %> setTimeout(function() { alert('<%= message %>'); }, 100); <% } %> </script> </body> </html>
最新发布
12-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值